#include "Date.h"

const char *jour[]={"lundi","mardi","mercredi","jeudi","vendredi","samedi","dimanche"};
const char *mois[]={"janvier","fevrier","mars","avril","mai","juin","juillet","aout","septembre","octobre","novembre","decembre"};



/* *********************************************************************
         	 Fonction de la structure Date
********************************************************************* */

void initDat(Date *date, const unsigned char jour, const unsigned char mois,
             const unsigned int annee,const unsigned char heure,const unsigned char minute)
{
  if(date)
    {
        setJourDat(date,jour);
        setMoisDat(date,mois);
        setAnneeDat(date,annee);
        setHeureDat(date,heure);
        setMinuteDat(date,minute);
    }
}

void libDat(Date *date)
{
  if(date)
    {
        date->jour  = 0;
        date->mois  = 0;
        date->annee = 0;
        date->heure = 0;
        date->minute= 0;
    }
}

Date * creDat(const unsigned char jour, const unsigned char mois,
              const unsigned int annee,const unsigned char heure,const unsigned char minute)
{
  Date * date = (Date *) calloc (1 , sizeof(Date));
  if(date == NULL)
    {
      fprintf(stderr, "Mémoire insuffisante");
      exit(EXIT_FAILURE);
    }
  initDat(date, jour, mois, annee, heure, minute);
  return date;
}

void detDat(Date **date)
{
  if(date)
    {
      libDat((*date));
      free((*date));
      (*date) = NULL;
    }
}
/*
void copier_Dat(Date ** dateDest, const Date * dateSrc)
{
  if((*dateDest) == NULL)
    {
      (*dateDest) = creDat();
    }
  if(dateSrc)
    {
      setJourDat((*dateDest), getJourDat(dateSrc));
      setMoisDat((*dateDest), getMoisDat(dateSrc));
      setAnneeDat((*dateDest), getAnneeDat(dateSrc));
      setHeureDat((*dateDest), getHeureDat(dateSrc));
      setMinuteDat((*dateDest), getMinuteDat(dateSrc));
    }
  else
    {
      detDat(dateDest);
      (*dateDest) = NULL;
    }
}
*/
/* *********************************************************************
           Accesseurs et Mutateurs de la structure Date
********************************************************************* */

unsigned char getJourDat(const Date *date)
{
  if(date)
    return date->jour;
  return 0;
}

void setJourDat(Date *date, const unsigned char jour)
{
  if(date)
    {
      if(jour == 0)
	    return;
      if(date->mois == 1 || date->mois == 3
	  || date->mois == 5 || date->mois == 7
	  || date->mois == 8 || date->mois == 10
	  || date->mois == 12)
	  {
	    if(jour > 31)
	      return;
	  }
	  else
	  {
     	if(jour > 30)
	      return;
      }
      if(date->mois == 2 && jour > 29)
	    return;
      date->jour = jour;
    }
}

unsigned char getMoisDat(const Date *date)
{
  if(date)
    return date->mois;
  return 0;
}


void setMoisDat(Date *date, const unsigned char mois)
{
  if(date)
    {
      if(mois > 0 && mois < 13)
        date->mois = mois;
    }
}

unsigned int getAnneeDat(const Date *date)
{
  if(date)
    return date->annee;
  return 0;
}


void setAnneeDat(Date *date, const unsigned int annee)
{
  if(date)
    {
      date->annee = annee;
    }

}

unsigned char getHeureDat(const Date *date)
{
  if(date)
    return date->heure;
  return 0;
}

void setHeureDat(Date *date, const unsigned char heure)
{
  if(date)
    {
      if(heure > 0 && heure < 24)
        date->heure = heure;
    }
}

unsigned char getMinuteDat(const Date *date)
{
  if(date)
    return date->minute;
  return 0;
}

void setMinuteDat(Date *date, const unsigned char minute)
{
  if(date)
    {
      if(minute>=0 && minute<61)
        date->minute = minute;
    }
}

char * getDateDat(const Date *date)
{
  char * chaine;
  chaine = (char *) calloc ( 12 , sizeof(char));
  sprintf(chaine, "%d/%d/%d", getJourDat(date), getMoisDat(date), getAnneeDat(date));
  return chaine;
}

char * getTempsDat(const Date *date)
{
  char * chaine;
  chaine = (char *) calloc ( 5, sizeof(char));
  sprintf(chaine, "%d:%d", getHeureDat(date), getMinuteDat(date));
  return chaine;
}

char * getDate(const Date *date)
{
  char * chaine;
  chaine = (char *) calloc ( 18 , sizeof(char));
  sprintf(chaine, "%d/%d/%d %d:%d",getJourDat(date), getMoisDat(date), getAnneeDat(date)
          , getHeureDat(date), getMinuteDat(date));
  return chaine;
}

void affDat(const Date *date)
{
    char *d;
    d = getDate(date);
    printf("\nDate : %s", d);
    /*free(d);*/
}

void regDat()
{
    char *d, *t;
    Date * date;
    date = creDat(21,11,2011,13,30);
    d = getDate(date);
    t = getTempsDat(date);
    printf("\nDate : %s",d);
    printf("\nTemps : %s\n",t);
    detDat(&date);
    free(d);
    free(t);
}

